<?php

namespace Admin\Controller;

use function PHPSTORM_META\elementType;
use Think\Controller;

class MessageController extends CommenController
{

    /***
     * 系统消息
     */
    public function sys_msg()
    {
        if (empty($_POST)) {
            $msg = M('merchant_msg', '', 'XYC')->where(['status' => 1, 'shop_identifier' => $_SESSION['shop_identifier']])->order('id desc')->field('content,id')->select();
            $this->assign(array('msg' => $msg));
            $this->display();
            die;
        }
        $mobiles = I('mobiles');//发送对象，逗号隔开的多个
        $content = I('content');
        $mobile = explode(',', $mobiles);
        $user_id = M('user', '', 'XYC')->where(['mobile' => array('in', $mobile)])->field('id,mobile')->select();
        $jg_api = M('merchant', '', 'XYC')->where(['shop_identifier' => $_SESSION['shop_identifier']])->field('jg_api,jg_secret')->find();


        foreach ($user_id as $k => $v) {
            $send = MycommonController::qxf_push($content, $v['id'], '', '', $jg_api['jg_api'], $jg_api['jg_secret']);
            add_control_log($_SESSION['shop_identifier'], $_SESSION['name'], '给用户' . $v['mobile'] . '推送消息', 3);
        }

        if ($send) {
            $this->ajaxReturn(array('code'=>'1001','result'=>'推送成功'));
            die;

        } else {
            $this->ajaxReturn(array('code'=>'1002','result'=>'推送失败'));
            die;
        }
    }

    /***
     * 系统消息添加
     */
    public function add_msg()
    {
        if (empty($_POST)) {
            $this->display();
            die;
        }

        $content = I('content');
        $arr = array('content' => $content, 'shop_identifier' => $_SESSION['shop_identifier'], 'account' => $_SESSION['name'], 'ctime' => time());
        $add = M('merchant_msg', '', 'XYC')->add($arr);
        if ($add) {
            add_control_log($_SESSION['shop_identifier'], $_SESSION['name'], '添加系统消息-' . $content, 2);
            $this->ajaxReturn(array('code' => '1001', 'result' => '成功'));
            die;
        }
        $this->ajaxReturn(array('code' => '1002', 'result' => '失败'));
        die;
    }

    /***
     * 用户反馈
     */
    public function user_feedback()
    {
        $start = I('start');
        $end = I('end');
        $start_time = strtotime($start);
        $end_time = strtotime($end);
        $search = I('key');
        $where = [];
        $where['b.shop_identifier'] = array(['eq', $_SESSION['shop_identifier']]);
        if (!empty($start) || !empty($end)) {
            if (empty($start)) {
                $where['a.ctime'] = array('elt', $end_time);
            } else if (empty($end)) {
                $where['a.ctime'] = array('egt', $start_time);
            } else {
                $where['a.ctime'] = array('between', array($start_time, $end));
            }
        }

        if (!empty($search)) {
            $where['b.mobile|b.username|a.content'] = array('like', '%' . $search . '%');
        }
        $search_arr = array('start' => $start, 'end' => $end, 'key' => $search);
        $field = 'b.username,b.mobile,a.content,a.ctime,a.id,a.status';
        $tol = M('user_feedback', '', 'XYC')->join('a join user b on b.id=a.user_id')->where($where)->count();
        $row = '10';
        $page = new PageController($tol, $row);
        $fpage = $page->fpage();

        $data = M('user_feedback', '', 'XYC')->join('a join user b on b.id=a.user_id')->limit($page->listfirst, $page->listRows)->order('a.id desc')->where($where)->field($field)->select();
        foreach ($data as $k => $v) {
            $data[$k]['ctime'] = date('Y-m-d H:i:s', $v['ctime']);
            if($v['status']==1)
            {
                $data[$k]['status']='已处理';
            }else{
                $data[$k]['status']='未处理';
            }
        }

        $this->assign(array(
            'data' => $data,
            'key' => $search,
            'fpage' => $fpage,
            'search' => $search_arr,
            'page_first' => $page->listfirst,
            'tol' => $tol
        ));
        $this->display();
    }

    /**
     * @param:
     * @param:详情反馈
     */
    public function feedback_detail()
    {
        $id = I('id');
        $data = M('user_feedback', '', 'XYC')->join('a join user b on b.id=a.user_id')->where(['a.id' => $id])->field('a.status,a.id as f_id,a.content,a.img,b.username,b.id as u_id,a.ctime')->find();
        $data['img'] = explode(',', $data['img']);
        $this->assign(array('detail' => $data));
        $this->display();

    }

    /***
     * @param:
     * @param:用户反馈处理
     */
    public function feedback_solve(){
        $id=I('id');
        $update=M('user_feedback','','XYC')->where(['id'=>$id])->setField('status',1);
        if($update){
            add_control_log($_SESSION['shop_identifier'], $_SESSION['name'],'处理用户反馈'.$id, 2);
            $this->ajaxReturn(array('code'=>'1001','result'=>'处理成功'));
            die;
        }
        $this->ajaxReturn(array('code'=>'1001','result'=>'处理失败'));
        die;
    }

}